﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrderManagementForm
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        //添加按钮
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2=new Form2();
            this.Hide();
            f2.ShowDialog();
            this.Dispose();
        }

        //修改按钮
        private void button2_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            this.Hide();
            f3.ShowDialog();
            this.Dispose();
        }

        //查询订单按钮
        private void button3_Click(object sender, EventArgs e)
        {
            string str1 = comboBox1.Text;
            string str2 = textBox1.Text;
            bool find = false;
            if (str1 == "")
            {
                MessageBox.Show("请选择查询方式！");
            }

            else if (OrderManagement.OrderItem != null) {
                    if (str1 == "订单号")
                    {
                        int id1 = Convert.ToInt32(str2);
                        foreach (OrderManagement.orderitem A in OrderManagement.OrderItem)
                        {
                            if (A.id == id1)
                            {
                                MessageBox.Show("你查询的订单内容为：\n 订单号：" + A.id + " 客户：" +
                                    A.customer + " 商品名称：" + A.goods + " 单价：" + A.price + " 数目：" + A.amount
                                    + "  金额：" + A.price * A.amount);
                            find = true;
                            }
                        }
                    if (!find)
                    {
                            MessageBox.Show("不存在你查询的订单！");
                    }
                    }

                    else if (str1 == "客户")
                    {
                        string customer1 = Convert.ToString(str2);
                        foreach (OrderManagement.orderitem A in OrderManagement.OrderItem)
                        {
                            if (A.customer == customer1)
                            {
                                MessageBox.Show("你查询的订单内容为：\n 订单号：" + A.id + " 客户：" +
                                    A.customer + " 商品名称：" + A.goods + " 单价：" + A.price + " 数目：" + A.amount
                                    + "  金额：" + A.price * A.amount);
                            find = true;
                            }
                        }
                    if (!find)
                    {
                        MessageBox.Show("不存在你查询的订单！");
                    }
                }
                    else 
                    {
                        string goods1 = Convert.ToString(str2);
                        foreach (OrderManagement.orderitem A in OrderManagement.OrderItem)
                        {
                            if (A.customer == goods1)
                            {
                                MessageBox.Show("你查询的订单内容为：\n 订单号：" + A.id + " 客户：" +
                                    A.customer + " 商品名称：" + A.goods + " 单价：" + A.price + " 数目：" + A.amount
                                    + "  金额：" + A.price * A.amount);
                            find = true;
                            }
                        }
                    if (!find)
                    {
                        MessageBox.Show("不存在你查询的订单！");
                    }
                }
                
            }
                else
                    MessageBox.Show("不存在你查询的订单！");
        }

        //删除订单按钮
        private void button4_Click(object sender, EventArgs e)
        {
            string str3 = textBox2.Text;
            bool moved = false;
            int  index=0;
            if ( str3== "")
            {
                MessageBox.Show("请输入订单号！");
            }
            else 
            {
                int id3 = Convert.ToInt32(str3);
                foreach (OrderManagement.orderitem A in OrderManagement.OrderItem)
                {
                    if (A.id == id3)
                    {

                        index= OrderManagement.OrderItem.IndexOf(A);
                        moved = true;
                    }
                }

                if (moved) { 
                    OrderManagement.OrderItem.RemoveAt(index);
                    MessageBox.Show("删除成功！");
                }
                      
                else
                    MessageBox.Show("不存在你要删除的订单！");
            }

        }

        //关闭程序按钮
        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
